/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Main;

import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;

/**
 *
 * @author gean
 */
public class Chambre extends javax.swing.JFrame {

    private final Connection conn;
    private ResultSet rs=null;
    
    public Chambre() {
        initComponents();
        conn = gestion_chambes_hotel.Gestion_Chambes_Hotel.DB_Connection();
        mise_a_jour();
        non.isSelected();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        Num_Chambre_Client = new javax.swing.JTextField();
        Etage = new javax.swing.JComboBox();
        oui = new javax.swing.JRadioButton();
        non = new javax.swing.JRadioButton();
        Type_Chambre = new javax.swing.JComboBox();
        num_ch = new javax.swing.JLabel();
        marier = new javax.swing.JLabel();
        etage = new javax.swing.JLabel();
        type_ch = new javax.swing.JLabel();
        num_chambre = new javax.swing.JLabel();
        etage_ar = new javax.swing.JLabel();
        type_ch_ar = new javax.swing.JLabel();
        marier_ar = new javax.swing.JLabel();
        num_ch_ar = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Chambre_Table = new javax.swing.JTable();
        ajouter = new javax.swing.JButton();
        supprimer = new javax.swing.JButton();
        modifier = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("                                                                    Espace de Chambres");
        setMaximumSize(new java.awt.Dimension(1000, 535));
        setMinimumSize(new java.awt.Dimension(970, 535));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });
        getContentPane().setLayout(null);

        jPanel1.setLayout(null);

        Num_Chambre_Client.setFont(new java.awt.Font("Abyssinica SIL", 1, 14)); // NOI18N
        Num_Chambre_Client.setBorder(null);
        Num_Chambre_Client.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Num_Chambre_ClientActionPerformed(evt);
            }
        });
        jPanel1.add(Num_Chambre_Client);
        Num_Chambre_Client.setBounds(210, 10, 160, 30);

        Etage.setFont(new java.awt.Font("Abyssinica SIL", 1, 14)); // NOI18N
        Etage.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2" }));
        jPanel1.add(Etage);
        Etage.setBounds(530, 20, 120, 27);

        buttonGroup1.add(oui);
        oui.setText("Oui");
        jPanel1.add(oui);
        oui.setBounds(100, 90, 70, 23);

        buttonGroup1.add(non);
        non.setText("Non");
        jPanel1.add(non);
        non.setBounds(180, 90, 90, 23);

        Type_Chambre.setFont(new java.awt.Font("Abyssinica SIL", 1, 14)); // NOI18N
        Type_Chambre.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4" }));
        jPanel1.add(Type_Chambre);
        Type_Chambre.setBounds(530, 90, 120, 27);

        num_ch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/num_chambre.png"))); // NOI18N
        jPanel1.add(num_ch);
        num_ch.setBounds(10, 20, 148, 13);

        marier.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/status.png"))); // NOI18N
        jPanel1.add(marier);
        marier.setBounds(20, 90, 47, 20);

        etage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/etage.png"))); // NOI18N
        jPanel1.add(etage);
        etage.setBounds(470, 20, 50, 20);

        type_ch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/type_chambre.png"))); // NOI18N
        jPanel1.add(type_ch);
        type_ch.setBounds(410, 100, 108, 15);

        num_chambre.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt1.png"))); // NOI18N
        jPanel1.add(num_chambre);
        num_chambre.setBounds(190, 0, 220, 50);

        etage_ar.setFont(new java.awt.Font("Amiri", 1, 16)); // NOI18N
        etage_ar.setText("الطابق :");
        jPanel1.add(etage_ar);
        etage_ar.setBounds(661, 15, 60, 40);

        type_ch_ar.setFont(new java.awt.Font("Amiri", 1, 16)); // NOI18N
        type_ch_ar.setText("نوع الغرفة :");
        jPanel1.add(type_ch_ar);
        type_ch_ar.setBounds(660, 85, 80, 40);

        marier_ar.setFont(new java.awt.Font("Amiri", 1, 16)); // NOI18N
        marier_ar.setText("حالة الغرفة :");
        jPanel1.add(marier_ar);
        marier_ar.setBounds(280, 90, 100, 20);

        num_ch_ar.setFont(new java.awt.Font("Amiri", 1, 16)); // NOI18N
        num_ch_ar.setText("رقم الغرفة :");
        jPanel1.add(num_ch_ar);
        num_ch_ar.setBounds(420, 5, 90, 40);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Accueil/wallhaven-19788.jpg"))); // NOI18N
        jPanel1.add(jLabel1);
        jLabel1.setBounds(0, 0, 750, 190);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(40, 20, 740, 180);

        Chambre_Table.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
        Chambre_Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        Chambre_Table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Chambre_TableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(Chambre_Table);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(10, 210, 980, 314);

        ajouter.setBackground(new java.awt.Color(0, 102, 102));
        ajouter.setFont(new java.awt.Font("DejaVu Sans Condensed", 1, 14)); // NOI18N
        ajouter.setForeground(new java.awt.Color(255, 255, 255));
        ajouter.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Buttons/Modifier_Chambre.png"))); // NOI18N
        ajouter.setText("Ajouter");
        ajouter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ajouterActionPerformed(evt);
            }
        });
        getContentPane().add(ajouter);
        ajouter.setBounds(820, 130, 120, 31);

        supprimer.setBackground(new java.awt.Color(204, 0, 0));
        supprimer.setFont(new java.awt.Font("DejaVu Sans Condensed", 1, 14)); // NOI18N
        supprimer.setForeground(new java.awt.Color(255, 255, 255));
        supprimer.setText("Supprimer");
        supprimer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                supprimerActionPerformed(evt);
            }
        });
        getContentPane().add(supprimer);
        supprimer.setBounds(820, 40, 120, 30);

        modifier.setBackground(new java.awt.Color(102, 102, 102));
        modifier.setFont(new java.awt.Font("DejaVu Sans Condensed", 1, 14)); // NOI18N
        modifier.setForeground(new java.awt.Color(255, 255, 255));
        modifier.setText("Modifier");
        modifier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifierActionPerformed(evt);
            }
        });
        getContentPane().add(modifier);
        modifier.setBounds(820, 80, 120, 30);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Accueil/wallhaven-19788.jpg"))); // NOI18N
        getContentPane().add(jLabel5);
        jLabel5.setBounds(0, 0, 1000, 560);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents
    private void Mettre_a_jour()
    {
            try{
            String msj="SELECT * FROM chambre";
            PreparedStatement ps1 = conn.prepareStatement(msj);
            rs=ps1.executeQuery();
            Chambre_Table.setModel(DbUtils.resultSetToTableModel(rs));
            
        }
        catch(SQLException e){
            
            JOptionPane.showMessageDialog(null,e);
        }
    }
    
    public void Traduire()
    {
        etage.setVisible(false);
        num_ch.setVisible(false);
        type_ch.setVisible(false);
        marier.setVisible(false);
        
        
        etage_ar.setVisible(true);
        type_ch_ar.setVisible(true);
        marier_ar.setVisible(true);
        Num_Chambre_Client.setText("رقم الغرفة ... ");
        num_ch_ar.setVisible(true);
        ajouter.setText("اظافة");
        supprimer.setText("حدف");
        modifier.setText("تعديل");
        oui.setText("متاحة");
        non.setText("غير متاحة");
    }
    
    public void visiblity()
    {
    
        etage_ar.setVisible(false);
        type_ch_ar.setVisible(false);
        marier_ar.setVisible(false);
        num_ch_ar.setVisible(false);
    }
    
    
    private void ajouterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ajouterActionPerformed
        tester();
        Mettre_a_jour();
    }//GEN-LAST:event_ajouterActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        mise_a_jour();
    }//GEN-LAST:event_formWindowActivated

    private void Num_Chambre_ClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Num_Chambre_ClientActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_Num_Chambre_ClientActionPerformed

    private void supprimerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_supprimerActionPerformed
        
        
              String id=Num_Chambre_Client.getText();
               
               
                      try {
                            String sql0="select * from chambre where NUMERO_CHAMBRE='"+id+"'";
                            String sql2 = "delete FROM chambre where NUMERO_CHAMBRE='"+id+"'";
                            
                            PreparedStatement ps0 = conn.prepareStatement(sql0);
                            PreparedStatement ps3 = conn.prepareStatement(sql2);
                            
                            rs=ps0.executeQuery();
                            if(rs.next())
                                
                                {
                                    if(JOptionPane.showConfirmDialog(null, "Supprimer","Vous etes sur?",JOptionPane.YES_NO_CANCEL_OPTION)==JOptionPane.YES_OPTION){
                                      
                                      ps3.executeUpdate();
                                      JOptionPane.showMessageDialog(null, "Supprimer!");
                                    }
                                
                                }
                            
                      
        
                }catch(SQLException | HeadlessException e){
                    
                    JOptionPane.showMessageDialog(null, e.getMessage());
            }
    }//GEN-LAST:event_supprimerActionPerformed

    private void Chambre_TableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Chambre_TableMouseClicked
        
        try {
            
            int row =Chambre_Table.getSelectedRow();
            String Table_click=(Chambre_Table.getModel().getValueAt(row, 0).toString());
            
            String sql0="select * from chambre where NUMERO_CHAMBRE='"+Table_click+"'";
            
            PreparedStatement ps0 = conn.prepareStatement(sql0);
            rs=ps0.executeQuery();
            
            
            if(rs.next())
            {
                String num=rs.getString("NUMERO_CHAMBRE");
                Num_Chambre_Client.setText(num);
            
            }
            
        } catch (Exception e) {
            
                JOptionPane.showInternalMessageDialog(null,e.getMessage());
            
        }
        
        
    }//GEN-LAST:event_Chambre_TableMouseClicked

    private void modifierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifierActionPerformed
        Modifier_Chambre();
        mise_a_jour();
    }//GEN-LAST:event_modifierActionPerformed

    /**
     * @param args the command line arguments
     */
    
    private void mise_a_jour()
        {
        
            
            
        try{
            String sql="SELECT * FROM chambre";
            PreparedStatement ps1 = conn.prepareStatement(sql);
            rs=ps1.executeQuery();
            Chambre_Table.setModel(DbUtils.resultSetToTableModel(rs));
            
        }
        catch(SQLException e){
            
            JOptionPane.showMessageDialog(null,e);
        }
        
    
        }
    
    private void Ajouter_Chambre()
    {
        
        String status ="";
       if(oui.isSelected())
        {
            status ="oui";
        }
       if(non.isSelected()){
       
           status ="non";
       }
       
        try {
                String type=(Type_Chambre.getSelectedItem().toString());
                String sql="insert into chambre(NUMERO_CHAMBRE,TYPE_CHAMBRE,STATUS_CHAMBRE,ETAGE_CHAMBRE) values(?,?,?,?)";
                
                PreparedStatement ps =conn.prepareStatement(sql);
                        ps.setString(1, Num_Chambre_Client.getText());
                        ps.setInt(2,Integer.parseInt(type));
                        ps.setString(3, status);
                        ps.setString(4, Etage.getSelectedItem().toString());
                        
                        
                        ps.executeUpdate();
                        JOptionPane.showMessageDialog(null, "Les infos est ajouter avec succes");
                        
            
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e.getMessage()+"Un erreur dans la syntaxe");
        }
        
    }
    
    private void Modifier_Chambre()
    {
        
        String status ="";
       if(oui.isSelected())
        {
            status ="oui";
        }
       if(non.isSelected()){
       
           status ="non";
       }
       String num_chambre=Num_Chambre_Client.getText();
        try {
                String type=(Type_Chambre.getSelectedItem().toString());
                String sql="update chambre set NUMERO_CHAMBRE=?,TYPE_CHAMBRE=?,STATUS_CHAMBRE=?,ETAGE_CHAMBRE=? where NUMERO_CHAMBRE='"+num_chambre+"'";
                
                PreparedStatement ps =conn.prepareStatement(sql);
                        ps.setString(1, Num_Chambre_Client.getText());
                        ps.setInt(2,Integer.parseInt(type));
                        ps.setString(3, status);
                        ps.setString(4, Etage.getSelectedItem().toString());
                        
                        
                        ps.executeUpdate();
                        JOptionPane.showMessageDialog(null, "les infos est modification avec succes");
                        
            
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e.getMessage()+"Un erreur dans la syntaxe");
        }
        
    }
    
    private void tester()
        {
               boolean x=false;
               if(Num_Chambre_Client.getText().equals(""))
                   {
                       JOptionPane.showMessageDialog(null, "Entrer le numero du chambre !");
                       x=true;
                   }
               if(x==false)
                {
                    Ajouter_Chambre();
                }
               
        }
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Chambre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Chambre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Chambre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Chambre.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Chambre().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Chambre_Table;
    private javax.swing.JComboBox Etage;
    private javax.swing.JTextField Num_Chambre_Client;
    private javax.swing.JComboBox Type_Chambre;
    private javax.swing.JButton ajouter;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JLabel etage;
    private javax.swing.JLabel etage_ar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel marier;
    private javax.swing.JLabel marier_ar;
    private javax.swing.JButton modifier;
    private javax.swing.JRadioButton non;
    private javax.swing.JLabel num_ch;
    private javax.swing.JLabel num_ch_ar;
    private javax.swing.JLabel num_chambre;
    private javax.swing.JRadioButton oui;
    private javax.swing.JButton supprimer;
    private javax.swing.JLabel type_ch;
    private javax.swing.JLabel type_ch_ar;
    // End of variables declaration//GEN-END:variables
}
